1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#![allow(non_camel_case_types, non_snake_case)]

use crate::decl::*;
use crate::kernel::ffi_types::*;
use crate::ole::privs::*;
use crate::prelude::*;
use crate::vt::*;

/// [`IGraphBuilder`](crate::IGraphBuilder) virtual table.
#[repr(C)]
pub struct IGraphBuilderVT {
	pub IFilterGraphVT: IFilterGraphVT,
	pub Connect: fn(COMPTR, COMPTR, COMPTR) -> HRES,
	pub Render: fn(COMPTR, COMPTR) -> HRES,
	pub RenderFile: fn(COMPTR, PCSTR, PCSTR) -> HRES,
	pub AddSourceFilter: fn(COMPTR, PCSTR, PCSTR, *mut COMPTR) -> HRES,
	pub SetLogFile: fn(COMPTR, HANDLE) -> HRES,
	pub Abort: fn(COMPTR) -> HRES,
	pub ShouldOperationContinue: fn(COMPTR) -> HRES,
}

com_interface! { IGraphBuilder: "56a868a9-0ad4-11ce-b03a-0020af0ba770";
	/// [`IGraphBuilder`](https://learn.microsoft.com/en-us/windows/win32/api/strmif/nn-strmif-igraphbuilder)
	/// COM interface over [`IGraphBuilderVT`](crate::vt::IGraphBuilderVT).
	///
	/// Automatically calls
	/// [`IUnknown::Release`](https://learn.microsoft.com/en-us/windows/win32/api/unknwn/nf-unknwn-iunknown-release)
	/// when the object goes out of scope.
	///
	/// # Examples
	///
	/// ```no_run
	/// use winsafe::{self as w, prelude::*, co};
	///
	/// let obj = w::CoCreateInstance::<w::IGraphBuilder>(
	///     &co::CLSID::FilterGraph,
	///     None,
	///     co::CLSCTX::INPROC_SERVER,
	/// )?;
	/// # w::HrResult::Ok(())
	/// ```
}

impl dshow_IFilterGraph for IGraphBuilder {}
impl dshow_IGraphBuilder for IGraphBuilder {}

/// This trait is enabled with the `dshow` feature, and provides methods for
/// [`IGraphBuilder`](crate::IGraphBuilder).
///
/// Prefer importing this trait through the prelude:
///
/// ```no_run
/// use winsafe::prelude::*;
/// ```
pub trait dshow_IGraphBuilder: dshow_IFilterGraph {
	fn_com_noparm! { Abort: IGraphBuilderVT;
		/// [`IGraphBuilder::Abort`](https://learn.microsoft.com/en-us/windows/win32/api/strmif/nf-strmif-igraphbuilder-abort)
		/// method.
	}

	/// [`IGraphBuilder::AddSourceFilter`](https://learn.microsoft.com/en-us/windows/win32/api/strmif/nf-strmif-igraphbuilder-addsourcefilter)
	/// method.
	#[must_use]
	fn AddSourceFilter(&self,
		file_name: &str,
		filter_name: &str,
	) -> HrResult<IBaseFilter>
	{
		let mut queried = unsafe { IBaseFilter::null() };
		ok_to_hrresult(
			unsafe {
				(vt::<IGraphBuilderVT>(self).AddSourceFilter)(
					self.ptr(),
					WString::from_str(file_name).as_ptr(),
					WString::from_str(filter_name).as_ptr(),
					queried.as_mut(),
				)
			},
		).map(|_| queried)
	}

	/// [`IGraphBuilder::Connect`](https://learn.microsoft.com/en-us/windows/win32/api/strmif/nf-strmif-igraphbuilder-connect)
	/// method.
	fn Connect(&self,
		pin_out: &impl dshow_IPin,
		pin_in: &impl dshow_IPin,
	) -> HrResult<()>
	{
		ok_to_hrresult(
			unsafe {
				(vt::<IGraphBuilderVT>(self).Connect)(
					self.ptr(),
					pin_out.ptr(),
					pin_in.ptr(),
				)
			},
		)
	}

	/// [`IGraphBuilder::Render`](https://learn.microsoft.com/en-us/windows/win32/api/strmif/nf-strmif-igraphbuilder-render)
	/// method.
	fn Render(&self, pin_out: &impl dshow_IPin) -> HrResult<()> {
		ok_to_hrresult(
			unsafe {
				(vt::<IGraphBuilderVT>(self).Render)(self.ptr(), pin_out.ptr())
			},
		)
	}

	/// [`IGraphBuilder::RenderFile`](https://learn.microsoft.com/en-us/windows/win32/api/strmif/nf-strmif-igraphbuilder-renderfile)
	/// method.
	fn RenderFile(&self, file: &str) -> HrResult<()> {
		ok_to_hrresult(
			unsafe {
				(vt::<IGraphBuilderVT>(self).RenderFile)(
					self.ptr(),
					WString::from_str(file).as_ptr(),
					std::ptr::null(),
				)
			},
		)
	}

	/// [`IGraphBuilder::SetLogFile`](https://learn.microsoft.com/en-us/windows/win32/api/strmif/nf-strmif-igraphbuilder-setlogfile)
	/// method.
	fn SetLogFile(&self, hfile: Option<&HFILE>) -> HrResult<()> {
		ok_to_hrresult(
			unsafe {
				(vt::<IGraphBuilderVT>(self).SetLogFile)(
					self.ptr(),
					hfile.map_or(std::ptr::null_mut(), |h| h.ptr()),
				)
			},
		)
	}

	/// [`IGraphBuilder::ShouldOperationContinue`](https://learn.microsoft.com/en-us/windows/win32/api/strmif/nf-strmif-igraphbuilder-shouldoperationcontinue)
	/// method.
	#[must_use]
	fn ShouldOperationContinue(&self) -> HrResult<bool> {
		okfalse_to_hrresult(
			unsafe {
				(vt::<IGraphBuilderVT>(self).ShouldOperationContinue)(self.ptr())
			},
		)
	}
}